<template>
  <div>
    <div class="header">
      <van-icon name="arrow-left" size="20" class="pad_10" @click="$router.go(-1)"/>
      <van-search
          shape="round"
          placeholder="请输入搜索关键词"
          class="flex_1"
          @focus="$router.push('/search')"
        />
      <van-button type="danger" size="small" class="mar_10 pad_10">搜索</van-button>
    </div>
    <div class="container">
      <div class="left">
        <div class="mar_10" :class="{active:active===index}" v-for="(item, index) in catelist " :key="item.id" @click="chose(index,item.id)">{{item.name}}</div>
      </div>
      <div class="right" v-if="items.length>0">
        <div class="item"
          v-for="item in items"
          :key="item.id"
          @click="$router.push({
            path:'/detail',
            query: {
              id:item.id
            }
          })"
        >
          <div ><img :src="item.pic" alt=""></div>
          <div class="smart">{{item.name}}</div>
        </div>
      </div>
      <div class="right" v-else>
        哦吼暂无
      </div>
    </div>
    <tabbar/>
  </div>
</template>

<script>
import { fetchCates, fetchItems } from '_api'
import Tabbar from '../../components/Tabbar.vue'
export default {
  data () {
    return {
      catelist: null,
      items: [],
      active: 0
    }
  },
  created () {
    this.fetchCates()
  },
  methods: {
    // 获取各自分类数据
    fetchItems (id) {
      fetchItems({
        categoryId: id
      }).then(res => {
        console.log(res.data)
        if (res.data.code === 0) {
          this.items = res.data.data.result
        } else {
          this.items = []
        }
      })
    },
    // 点击选择分类
    chose (index, id) {
      this.active = index
      this.fetchItems(id)
    },
    // 获取分类列表
    fetchCates () {
      fetchCates().then(res => {
        this.catelist = res.data.data
        this.fetchItems(this.catelist[0].id)
      })
    }
  },
  components: { Tabbar }
}
</script>

<style lang="scss" scoped>
.header{
  position: fixed;
  width: 100%;
  left: 0;
  top: 0;
  z-index: 1000;
  display: flex;
  font-size: 16px;
  align-items: center;
  background-color: white;
  .pad_10{
    padding: 0 10px;
  }
  .mar_10{
    margin: 0 10px;
  }
  .flex_1{
    flex: 1;
  }
}
.container{
  // 高度等会要去掉
  height: 100vh;
  padding-top: 54px;
  padding-bottom: 50px;
  display: flex;
  font-size: 18px;
  .left{
    width: 30%;
    overflow-y: auto;
    .mar_10{
      margin: 8px 0;
      border: 2px solid transparent;
    }
    .active{
      color: orangered;
      border-left: 2px solid orangered;
    }
  }
  .right{
    width: 70%;
    overflow-y: auto;
    display: flex;
    flex-wrap: wrap;
    align-content: flex-start;
    .item{
      box-sizing: border-box;
      width: 33.33%;
      padding: 2px;
      img{
        width: 100%;
        border-radius: 5px;
      }
      .smart{
        font-size: 12px;
      }
    }
  }
}
</style>
